package com.hexu.event.dao;

import com.hexu.event.entity.Attractions;
import com.hexu.event.entity.Comment;
import com.hexu.event.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CommentDao {

    //保存评论
    public boolean addComment(Comment comment){
        String sql = "insert into comment(user_phone_number,attractions_name,content,comment_time)"
                + " values (?,?,?,?);";
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);

            //View层set的数据
            ps.setString(1, comment.getUserPhoneNumber());
            ps.setString(2, comment.getAttractionsName());
            ps.setString(3,comment.getContent());
            ps.setString(4, comment.getCommentTime());

            ps.executeUpdate();

            //添加成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }

        //添加失败
        return false;
    }

    //返回单个景点的所有评论
    public List<Comment> getComment(String attractionName){
        List<Comment> list = new ArrayList<>();
        String sql = "select * from comment where attractions_name=? ";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1,attractionName);
            rs = ps.executeQuery();

            while (rs.next()){
                Comment comment = new Comment();
                comment.setId(rs.getInt("id"));
                comment.setAttractionsName(rs.getString("attractions_name"));
                comment.setUserPhoneNumber(rs.getString("user_phone_number"));
                comment.setContent(rs.getString("content"));
                comment.setCommentTime(rs.getString("comment_time"));

                list.add(comment);

            }
            return list;

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return null;
    }

    //返回单个景点的所有评论数
    public int commentsNumber(String attractionName){
        String sql = "select attractions_name from comment where attractions_name=?";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConn();
            //使游标可滚动
            ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            ps.setString(1,attractionName);
            rs = ps.executeQuery();

            // 将游标移动到最后一行上
            rs.last();
            //获取景点数
            int commentsCount = rs.getRow();
            //把光标移至第一行前
            rs.beforeFirst();

            return commentsCount;

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return 0;
    }
}
